//数据
const { aweme_list } = require('./data.json');
const download = require('download');
const fs = require('fs');
const md5 = require('md5');
//遍历数据
// let data = require('./test.json')['douyin'];
let data = [];

aweme_list.forEach(item => {
    let obj = {};
    //作者昵称
    obj.nickname = item.author.nickname;
    //作者图片
    obj.author_img = item.author.avatar_300x300.url_list[0];

    // //下载图片
    var dir = __dirname + '/../../public/images/profiles'
    var name = md5(obj.author_img) + '.jpeg';
    //下载图片
    var path = dir + '/' + name;
    //下载图片
    download(obj.author_img).pipe(fs.createWriteStream(path));
    //
    obj.author_img_local = '/images/profiles/'+name;

    //作者签名
    obj.signature = item.author.signature;

    //作品描述
    obj.desc = item.desc;
    //作品封面
    obj.cover = item.video.origin_cover.url_list[0];

    // //下载图片
    var dir = __dirname + '/../../public/images/cover'
    var name = md5(obj.cover) + '.jpeg';
    //下载图片
    var path = dir + '/' + name;
    //下载图片
    download(obj.cover).pipe(fs.createWriteStream(path));
    //
    obj.cover_local = '/images/cover/' + name;

    //作品视频
    obj.video = item.video.download_addr.url_list[0];
    //下载视频
    // //下载图片
    var dir = __dirname + '/../../public/videos/douyin2'
    var name = md5(obj.video) + '.mp4';
    //下载图片
    var path = dir + '/' + name;
    //下载图片
    download(obj.video).pipe(fs.createWriteStream(path));
    obj.video_local = '/videos/douyin2/' + name;
    //作品时长
    obj.duration = item.duration;
    //作品点赞数
    obj.star_count = item.statistics.digg_count
    //作品评论数
    obj.comment_count = item.statistics.comment_count;
    //作品收藏数
    obj.collect_count = item.statistics.collect_count
    //作品标签
    obj.tags = [];
    if (item.text_extra) {
        item.text_extra.forEach(v => {
            if (v.hashtag_name) obj.tags.push(v.hashtag_name)
        })
    }

    data.push(obj);
});

fs.writeFileSync(__dirname + '/douyin_11.json', JSON.stringify({ douyin: data }));
